<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.totoro.domain.Tutorial" %>
<%@ include file="/WEB-INF/jsp/include.jsp"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>指导老师评审表</title>
<link href="<%=basePath%>css/ls3.css" rel="stylesheet" type="text/css" />
<link href="<%=basePath%>images/skin.css" rel="stylesheet"
	type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript" src="<%=basePath%>js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
	$(function(){
		
	// 设置各按钮状态（是否可用）
		setButtonState();
		$("#numbers").html($.trim($("#remark").val()).length);
	
		//计算总成绩
		calculateTotalMark();
	
		$("select.mark").change(function() {
		    // 动态显示总成绩
		    calculateTotalMark();
		    setButtonState();
		});
	
		// 取消按钮点击时重新计算总成绩
		$("#cancel").click(function() {
		    calculateTotalMark();
		});
		
			//保存评语模板
		$("#saveAsTemplate").click(function() {
			  var templateContent = $.trim($("#remark").val());
			    
			  if (templateContent.length <= 0) {
			        alert("请先填写评语...");
			        return false;
			  }
	    
		    var defaultValue;
		    var selectedTemplate = $("#remarkTemplate").children(":selected");
		    if (selectedTemplate.val() == '0') {
		        defaultValue = "指导教师评语 -优秀";
		    } else {
		        defaultValue = $.trim(selectedTemplate.text());
		    }
		    
		    inputPrompt(defaultValue);
		    
		});
	
		// IE10中可以不用这个，其他版本求测试，先保留
		document.getElementById("remark").onpropertychange = function() {
		    setButtonState();
		    $("#numbers").html($.trim($("#remark").value()).length);
		};
		
		if (window.addEventListerner) {
		    document.getElementById("remark").addEventListener('input', function() {
		        setButtonState();
		        $("#numbers").html($.trim($("#remark").val()).length);
		    });
		}
	
	});
	
	//计算文本框字的个数
	function countChar(textareaName,spanName){  
 			document.getElementById(spanName).innerHTML = document.getElementById(textareaName).value.length;
		}  
	
	//计算总成绩
	function calculateTotalMark() {
	
		var totalMark = 0;
		if($("select.mark").length > 0) {
		    var currentValue;
		    $("select.mark").each(function() {
		        currentValue = parseInt($(this).children(":selected").val());
		        if (currentValue < 0) {
		            currentValue = 0;
		        }
		        totalMark += currentValue;
		    });
		} else {
		    $("input.mark").each(function() {
		        totalMark += parseInt($(this).val());
		    });
		}
		
		$("#totalMark").val(totalMark);
	}
	
	function setButtonState() {
		if ($('#state').val() == 'true') {
		    $("#remarkTemplate").attr("disabled", "disabled");
		    $("#submit1").attr("disabled", "disabled");
		    $("#submit2").attr("disabled", "disabled");
		    $("#cancel").attr("disabled", "disabled");
		    $("#btn1").attr("disabled", "disabled");
		    $("#saveAsTemplate").attr("disabled", "disabled");
		    $("#remark").attr("disabled", "disabled");
		} else {
		    if (isValidate()) {
		        $("#submit1").removeAttr("disabled");
		        $("#submit2").removeAttr("disabled");
		        $("#btn1").removeAttr("disabled");
		    } else {
		        $("#submit1").attr("disabled", "disabled");
		        $("#submit2").attr("disabled", "disabled");
		        $("#btn1").attr("disabled", "disabled");
		    }
		}
	}
	
		function isValidate() {
	
			var currentValue;
			var isValidate = true;
		
			$("select.mark").each(function() {
			    currentValue = parseInt($(this).children(":selected").val());
			    if (currentValue < 0) {
			        isValidate = false;
			        return;
			    }
			});
		
			if (!isValidate) {
			    return false;
			}
		
			if($.trim($("#remark").val()).length < 20) {
			    return false;
			}
		
			return true;
	    }
	
	var i = 1;
	function change() {
	if (i % 2 == 1)
		document.getElementById("box").style.display = "";
	if (i % 2 == 0)
		document.getElementById("box").style.display = "none";
	i++;
	}
	//保 存
	function sm1() {
		if(isValidate()){
			$("#form1").attr("action","../tutor/tutorReview.html");
			$("#form1").submit();
		}
	
	}
	//暂 存
	function sm2() {      
		$("#form1").attr("action","../tutor/tutorReviewTag.html");
		$("#form1").submit();
	}
	
	function toexcel(project_id,tutor_id) {
		var actionUrl = "../tutor/showTutorialByXls.html?project_id=" + project_id + "&&tutor_id=" + tutor_id; //你的action路径
		window.open(actionUrl);
	}
	
	var keyword;
	var isOK = false;
	var isDuplicated = false;
	
	function inputPrompt(defaultValue) {
		keyword = window.prompt("输入模板标题", defaultValue);
		if (keyword != null && $.trim(keyword).length > 0) {
		    $.ajax({
		        url: '../tutor/getRemarkTemplateKeywords.html',
		        data: {'keyword': keyword},
		        dataType: 'json',
		        type: 'POST'
		    }).done(function(data) {
		        isDuplicated = data.isDuplicated;
		            
		        if (isDuplicated) {
		            isOK = confirm("您输入的关键字已经存在，是否替换模板？\r\n点击确定直接替换，点击取消后修改关键字。");
		            if(!isOK) {
		                inputPrompt(defaultValue);
		            } else {
		                saveAsTemplate();
		            }
		        } else {
		            saveAsTemplate();
		        }
	    	});
		} else {
	    return;
		}
	    
	}
	
	function saveAsTemplate() {
	
		var selectedTemplate = $("#remarkTemplate").children(":selected");
		var templateContent = $.trim($("#remark").val());
		
		// isOK == true : 替换
		if (isOK) {
		    $.ajax({
		        url: '../tutor/saveAsTemplate.html',
		        data: {id: $.trim(selectedTemplate.val()), keyword: $.trim(keyword), description: templateContent},
		        dataType: 'json',
		        type: 'POST',
		        success: function(data) {
		            alert("保存成功!");
		        }
		    });
		} else {    // isOK == false : 添加
		    $.ajax({
		        url: '../tutor/saveAsTemplate.html',
		        data: {keyword: $.trim(keyword), description: templateContent},
		        dataType: 'json',
		        type: 'POST',
		        success: function(data) {
		            alert("保存成功!");
		        }
		    });
		}
	
	}
		
</script>
</head>
<body>
	<div id="contain2">
		<table width="100%" border="0" cellpadding="0" cellspacing="0"
			class="tab1">
			<tr>
				<td width="17" valign="top"
					background="<%=basePath%>images/mail_leftbg.gif"><img
					src="<%=basePath%>images/left-top-right.gif" width="17" height="29" /></td>
				<td valign="top" background="<%=basePath%>images/content-bg.gif">
					<table width="100%" height="31" border="0" cellpadding="0"
						cellspacing="0" class="left_topbg" id="table2">
						<tr>
							<td height="31"><div class="titlebt">
									<p>指导老师评审表</p>
								</div></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>

		<div id="main">
			<div class="main1">
				<p>山&nbsp;东&nbsp;建&nbsp;筑&nbsp;大&nbsp;学&nbsp;毕&nbsp;业&nbsp;论&nbsp;文&nbsp;指&nbsp;导&nbsp;教&nbsp;师&nbsp;评&nbsp;审&nbsp;表</p>
			</div>
			<div class="main2">
				<p>基本信息</p>
			</div>
			<div class="main3">
				<div class="bt22">
					<p>题目名称</p>
				</div>
				<div class="bt33">
					<p>学生姓名</p>
				</div>
				<div class="bt44">
					<p>班级</p>
				</div>
				<div class="bt55">
					<p>学号</p>
				</div>
			</div>
			<div class="main3">
				<div class="bt22">
					<p>${tutorial.project.title}</p>
				</div>
				<div class="bt33">
					<p>${tutorial.project.student.name}</p>
				</div>
				<div class="bt44">
					<p>${tutorial.project.student.studentClass.description}</p>
				</div>
				<div class="bt55">
					<p>${tutorial.project.student.no}</p>
				</div>
			</div>

			<!--<c:url var="action" value="/tutor/tutorReview.html" />-->
			<form:form modelAttribute="tutorial" action="${action}" method="post"
				name="form1" id="form1">
				<div class="main2">
					<p>指导教师评分（每项各占10分，总计40分）</p>
				</div>
				<div class="main6">
					<div class="bt8">
						<p>
							<br />工作量、工作态度
						</p>
					</div>
					<div class="bt8">
						<p>
							<br />基本理论、基本知识、基本技能和外语水平
						</p>
					</div>
					<div class="bt8">
						<p>
							<br />独立工作能力、分析与解决问题的能力
						</p>
					</div>
					<div class="bt8">
						<p>
							<br />完成任务情况及水平
						</p>
					</div>
					<div class="bt9">
						<p>
							<br />总计
						</p>
					</div>
				</div>
				<div class="main6">
					<form:hidden path="project.id" />
					<form:hidden path="tutor.id" />
					<form:hidden path="rank" />
					<form:hidden path="state" id="state" value="${tutorial.state}"/>
					<div class="bt8">
						<p>
							<br />
							<c:choose>
							  <c:when test="${tutorial.state == true}">
							    <form:input class="mark" path="workLoad" readonly="true" maxlength="4" />
							  </c:when>
							  <c:otherwise>
							    <form:select class="mark" id="workLoad" path="workLoad">
							        <form:option value="-1" label="-打分-" />
                                    <option value="10">10</option>
									<option value="9">9</option>
									<option value="8">8</option>
									<option value="7">7</option>
									<option value="6">6</option>
									<option value="5">5</option>
									<option value="4">4</option>
									<option value="3">3</option>
									<option value="2">2</option>
									<option value="1">1</option>
									<option value="0">0</option>
                                  </form:select>
							  </c:otherwise>
						    </c:choose>
						   <span>
						    <c:if test="${tutorial.workLoad>=0}">
									<script type="text/javascript">
										var value = ${tutorial.workLoad};
										$('#workLoad option[value='+value+']').attr("selected","selected");
									</script>
								</c:if>
						 </span>
						</p>
					</div>
					<div class="bt8">
						<p>
							<br />
							<c:choose>
							  <c:when test="${tutorial.state == true}">
							    <form:input class="mark" path="basicAbility" readonly="true" size="4" maxlength="4" />
							 </c:when>
							  <c:otherwise>
							     <form:select class="mark" id="basicAbility" path="basicAbility">
							        <form:option value="-1" label="-打分-" />
                                    <option value="10">10</option>
									<option value="9">9</option>
									<option value="8">8</option>
									<option value="7">7</option>
									<option value="6">6</option>
									<option value="5">5</option>
									<option value="4">4</option>
									<option value="3">3</option>
									<option value="2">2</option>
									<option value="1">1</option>
									<option value="0">0</option>
                                  </form:select>
							  </c:otherwise>
						    </c:choose>
						   <span>
						   	 <c:if test="${tutorial.basicAbility>=0}">
									<script type="text/javascript">
										var value = ${tutorial.basicAbility};
										$('#basicAbility option[value='+value+']').attr("selected","selected");
									</script>
								</c:if>
						   </span>
						</p>
					</div>
					<div class="bt8">
						<p>
							<br />
							<c:choose>
							  <c:when test="${tutorial.state == true}">
							   <form:input class="mark" path="workAbility" readonly="true" size="4" maxlength="4" />
							 </c:when>
							  <c:otherwise>
							     <form:select class="mark" id="workAbility" path="workAbility">
							        <form:option value="-1" label="-打分-" />
                                    <option value="10">10</option>
									<option value="9">9</option>
									<option value="8">8</option>
									<option value="7">7</option>
									<option value="6">6</option>
									<option value="5">5</option>
									<option value="4">4</option>
									<option value="3">3</option>
									<option value="2">2</option>
									<option value="1">1</option>
									<option value="0">0</option>
                                  </form:select>
							  </c:otherwise>					 
						    </c:choose>
						    <span>
						     <c:if test="${tutorial.workAbility>=0}">
									<script type="text/javascript">
										var value = ${tutorial.workAbility};
										$('#workAbility option[value='+value+']').attr("selected","selected");
									</script>
								</c:if>
						    </span>
						</p>
					</div>
					<div class="bt8">
						<p>
							<br />
							<c:choose>
							  <c:when test="${tutorial.state == true}">
							   <form:input class="mark" path="achievementLevel" readonly="true" size="4" maxlength="4" />
							  </c:when>
							  <c:otherwise>
							     <form:select class="mark" id="achievementLevel" path="achievementLevel">
							        <form:option value="-1" label="-打分-" />
                                    <option value="10">10</option>
									<option value="9">9</option>
									<option value="8">8</option>
									<option value="7">7</option>
									<option value="6">6</option>
									<option value="5">5</option>
									<option value="4">4</option>
									<option value="3">3</option>
									<option value="2">2</option>
									<option value="1">1</option>
									<option value="0">0</option>
                                  </form:select>
							  </c:otherwise>							  
						    </c:choose>
						    <span>
						    <c:if test="${tutorial.achievementLevel>=0}">
									<script type="text/javascript">
										var value = ${tutorial.achievementLevel};
										$('#achievementLevel option[value='+value+']').attr("selected","selected");
									</script>
								</c:if>
						    </span>
						</p>
					</div>
					<div class="bt9">
						<p>
							<br /> <!-- <input type="text"
								value="${tutorial.achievementLevel+tutorial.workAbility+tutorial.basicAbility+tutorial.workLoad}"
								size="4" maxlength="4" /> -->
								<input id="totalMark" type="text" readonly="readonly" size="4" value="0"/>
								<script type="text/javascript">
										var value1 = ${tutorial.workLoad};
										var value2 = ${tutorial.basicAbility};
										var value3 = ${tutorial.workAbility};
										var value4 = ${tutorial.achievementLevel};
										var value = parseInt(value1)+parseInt(value2)+parseInt(value3)+parseInt(value4);
										$('#totalMark').val(value+'');
								</script>
						</p>
					</div>
				</div>
				<div class="main7">
					<p>
						指导教师评语（包括工作态度，遵守纪律；&nbsp;&nbsp;基本理论、知识、技能及外语水平；独立工作能力和分析解决问题的能力；完成任务情况及<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;水平、能否参加答辩等方面）
					</p>
				</div>

				<div class="main8">
					<form:textarea id="remark" path="remark" name="py" cols="90"
						rows="6" onkeydown='countChar("remark","numbers");' onkeyup='countChar("remark","numbers");'/>
					<div id="remarkLength" style="margin: 6px; clear:both; font-size: 14px;">
                        <label style="color: green">字数至少20, 您已输入 </label><label id="numbers" style="color:red">0</label><label style="color:green"> 字</label>
                    </div>                 
				</div>

				<div class="main8">
					系统评语模板： <select id="remarkTemplate" name="name">
						<option value="0">-请选择-</option>
						<c:forEach items="${sys}" var="sys" varStatus="i">
							<option value="${sys.id}">${sys.keyword}</option>
						</c:forEach>
					</select>
					<input type="button" value="保存为模板" id="saveAsTemplate"/>
				</div>

				<p style="margin-left: 15px;">注：各专业应根据自己的具体情况，制定出适合本专业的毕业设计（论文）的具体要求和评分标准。</p>
				<p>
					<input name="w" id="toExcel" type="button" value="导出excel表"
						style="margin-left: 600px;"
						onclick="toexcel(${tutorial.project.id},${tutorial.tutor.id})" />
					<input name="submit1" id="submit1" type="button" value="保存" onclick="sm1()" />
					<input name="submit2" id="submit2" type="button" value="暂存" onclick="sm2()" />
					<input name="cancel" id="cancel" type="reset" value="取消" />
					<input name="btn1" id="btn1" type="button" value="打印" /> 	
				</p>
			</form:form>

		</div>
	</div>
	
	<script type="text/javascript">
	var checkText = $("#remarkTemplate").find("option:selected").text();//获取Select选择的text
	var checkValue = $("#remarkTemplate").val();  //获取Select选择的Value 
	//获取模板评语被选择的内容
	$("#remarkTemplate").change(function(){
		
		//指定向/sysRemarkTemplate/findById.html发送请求
		$.ajax({type:"get",
			url:"<%=basePath%>sysRemarkTemplate/findById.html",
			dataType:"json",
			data:"id="+$("#remarkTemplate").val()
			})
		//使用done()方法添加“执行成功”的回调函数
		.done(function(data,statusText){
			if(data.describtion!=null){
					$("#remark").val(data.describtion);
					setButtonState();
	                $("#numbers").html($.trim($("#remark").val()).length);
				}
		})
		.fail(function(){
			$("#remark").val("");
			setButtonState();
	        $("#numbers").html($.trim($("#remark").val()).length);
		});
		
		
	});
	//加载评语模板内容

	</script>
</body>
</html>